草庐IT

Java Spring resttemplate 字符编码

全部标签

json - 在字符串中插入一个变量

我有hostname和json格式的字符串。我想将hostname插入到该json格式字符串中的键的值字符串中。我的完整代码:funcpager()string{token:="xxxxxxxxxxx"url:="https://api.pagerduty.com/incidents"hostname,err:=os.Hostname()fmt.Println(hostname,err)jsonStr:=[]byte(`{"incident":{"type":"incident",**"title":"Dockerisdownon."+hostname,**"service":{"id

go - 如何取消转义html字符串中的引号

我在Go中有一个字符串如下:Helloworld!Google引号被转义了,我想得到没有反斜杠的字符串。我尝试使用html.UnescapeString但不是我想要的。我的问题有解决办法吗? 最佳答案 使用strings.NewReplacer()funcNewReplacer(oldnew...string)*Replacerpackagemainimport("bytes""fmt""log""strings""golang.org/x/net/html")funcmain(){consthtm=`Helloworld!Goog

go - 创建一个函数来测试从接口(interface)编码/解码

我想创建一个简单的函数来测试编码/解码记录是否按预期工作。我只是在这个例子中使用JSON:packagetestimport("encoding/json""fmt""testing""reflect""github.com/stretchr/testify/require")funcCheckRoundTripJSON(t*testing.T,recordinterface{}){data,err:=json.Marshal(record)require.NoError(t,err)fmt.Println("Record:",record,"wasencodedto:",data,"

sql - 将查询结果从结构转换为另一个 Golang 包的字符串

我在网上和SO中搜索了一个解决方案,但没有找到适用于返回值的解决方案。这是一个简单的sql查询,其中包含我要返回的多行。不包括错误处理:funcFetch(querystring)(string){typeUserstruct{idstringnamestring}rows,err:=db.Query(query)users:=make([]*User,0)forrows.Next(){user:=new(User)err:=rows.Scan(&user.id,&user.name)users=append(users,user)}return(users)}编译时出现这个错误:ca

regex - 如何匹配不包含特定字符串的字符串

我想在Go中编写regexp以匹配不包含特定子字符串(-numinput)且包含另一个特定字符串(-setup)的字符串。例如,对于inputStr以下类型的字符串不应匹配,因为存在-numinputstr="axxxyy-setupabc-numinput12345678aaa"以下类型的字符串应该匹配,因为-setup存在且-numinput不存在str="axxxyy-setupabcaaa"以下类型的字符串不应匹配,因为即使-numinput不存在,-setup也不存在str="axxxyyabcaaa"我看到了一些帖子,比如Regularexpressiontomatchal

variables - 如何通过连接两个字符串来创建动态变量名

我想通过连接当前日期来创建变量名,并为创建的变量名提供一个值。我的变量名应该类似于这样的"Key-2019-01"这样我就可以将值存储为varKey-2019-01="yes"我试过如下。packagemainimport("fmt""time""strconv""strings")funcmain(){currentMonth:=time.Now().Month()currentYear:=time.Now().Year()varmonth=int(currentMonth)varcurrentDate=strings.Join([]string{strconv.Itoa(curre

go - 成对迭代字符串数组以创建映射

这个问题在这里已经有了答案:Golang:convertslicesintomap(7个答案)关闭3年前。我有一个字符串数组,其长度始终是二的倍数。我想用这个数组创建一个map,这样myarr=["key1","val1","key2","val2",...]成为一张map,其中mymap:=mapify(myarr)mymap['key1']=="val1"mymap['key2']=="val2"在Python中我可以用下面的代码做到这一点mymap={}forx,yinzip(*[iter(myarr)]*2):mymap[x]=y

json - 将结构编码为 json Go

我在将结构编码为json时遇到问题,我的代码是typeMainStructurestruct{Textstringjson:"text,omitempty"Array[]TestArrayjson:"test_array,omitmepty"}typeTestArraystruct{ArrayTextstringjson:"array_text,omitempty"}funcmain(){Test:=MainStructure{Text:"test",Array:[{ArrayText:"test1"},{ArrayText:"test2"}]}body:=new(bytes.Buff

go - 如何将一段结构编码为有效的 JSON

我正在编写Golangapi和客户端,但无法从api中的结构片段中获取有效的json。我在客户端中得到的结果如下所示。[{0Mark1234false}{0John3456false}]我需要这个json看起来像[{"id":0,"name":Mark,"pin":1234,"active":false}{"id":0,"name":John,"pin":3456,"active":false}]我找不到向我展示如何正确编码的示例,尽管有警告,这不是我能找到的任何内容的重复。虽然我的客户端成功地将JSON解析回结构,但我还需要它将JSON返回给请求它的IOS客户端。流程是API->AP

go - Golang 结构体名称和类型后面的字符串是什么?

这个问题在这里已经有了答案:WhatisthethirdparameterofaGostructfield?(2个答案)Whatistheusageofbacktickingolangstructsdefinition?[duplicate](2个答案)StrangetypedefinitionsyntaxinGolang(name,thentype,thenstringliteral)(1个回答)GoStringaftervariabledeclaration(2个答案)StringliteralsinGOstructuredefinition[duplicate](1个回答)关闭3